'use strict';
const db = uniCloud.database()
const collection = db.collection('address')
exports.main = (event, context) => {
	console.log("结果：", event);
	switch(event.type) {
		case 'add': 
			return addAddress(event.dataObj)
		case 'get':
			return getList()
		case 'del': 
			return delList(event.data.id)
		case 'getId':
			return getIdData(event.id)
		case 'editId':
			return editIdData(event.id, event.dataObj)
	}
	
};

//添加新地址
async function addAddress(obj) {
	const res = await collection.add(obj)
	return res
}

//获取列表
async function getList() {
	const res = await collection.get()
	return res
}

//删除列表
async function delList(id) {
	const res = await collection.doc(id).remove()
	return res
}

//根据_id查询
async function getIdData(id) {
	return await collection.doc(id).get()
}

//编辑地址
async function editIdData(id, obj) {
	const res = await collection.doc(id).update({
		name: obj.name,
		phone: obj.phone,
		address: obj.address,
		juTi: obj.juTi
	})
	return res
}
